package cn.itsy.web.controller;

import cn.itsy.pojo.EmpAndUserAndRole;
import cn.itsy.pojo.SysUserRole;
import cn.itsy.pojo.User;
import cn.itsy.service.sys_user_role.SysUserRoleService;
import cn.itsy.service.sysrole.SysRoleService;
import cn.itsy.service.user.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description:
 * @author: MisterGay
 * @date: 2019-06-03 17:07
 */
@Controller
@RequestMapping(value = "user")
public class UserController {
    @Resource(name = "userServiceimpl")
    private UserService userService;

    @Resource(name = "sysRoleServiceimpl")
    private SysRoleService sysRoleService;

    @Resource(name = "sysUserRoleServiceimpl")
    private SysUserRoleService sysUserRoleService;

    @RequestMapping(value = "queryUserAll")
    @ResponseBody
    public List<User> queryUserAll() {
        String sql = "select * from user";
        List<User> users = this.userService.queryUserAll(sql);
        return users;
    }

    @RequestMapping(value = "queryEmpAndUserAndRoleAll", method = RequestMethod.GET)
    @ResponseBody
    public List<EmpAndUserAndRole> queryEmpAndUserAndRoleAll() {
        String sql = "select emp.*,sys_role.role_name,user.uname from " +
                " user,sys_role,sys_user_role,emp " +
                " where user.id=sys_user_role.user_id " +
                " and sys_role.id=sys_user_role.role_id " +
                " and emp.id=user.emp_id and user.is_status='Y' ";
        return this.userService.queryEmpAndUserAndRole(sql);
    }

    /**
     * 添加用户的方法
     */
    @RequestMapping(value = "saveUser", method = RequestMethod.GET)
    @ResponseBody
    public void saveUser(User user) {
        /**
         * 添加前判断有没有这个用户
         */
        String selectUserSql = "select * from user where uname=?";
        User user2 = this.userService.queryUserByuName(selectUserSql, user.getUname());
        if (user2 == null) {
            SysUserRole sysUserRole = new SysUserRole();
            String sql = "insert into user values(?,?,?,?,?,?)";
            this.userService.saveUser(sql, user);
            /**
             * 查询最新插入用户表的id值
             */
            User user1 = this.userService.queryUserLastId("select max(id) as id from user");
            if (user1 != null) {
                sysUserRole.setUserId(user1.getId());
            }
            String insertSql = "insert into sys_user_role values(?,?,?,?)";
            sysUserRole.setRoleId(user.getRoleId());
            sysUserRole.setIsEnabled("Y");
            this.sysUserRoleService.saveUserRole(insertSql, sysUserRole);
        } else {
            throw new RuntimeException("重复");
        }
    }

    /**
     * 修改用户及角色
     */
    @RequestMapping(value = "updateUser", method = RequestMethod.GET)
    @ResponseBody
    public void updateUserByuName(String uname, String roleName, String oldUname, String oldRoleName) {
        String updateUserSql = "update user set uname = ? where uname = ?";
        String updateRoleSql = "update sys_role set role_name = ? where role_name = ?";
        this.userService.updatUserByuName(updateUserSql, uname, oldUname);
        this.sysRoleService.updateSysRoleByName(updateRoleSql, roleName, oldRoleName);
    }

    /**
     * 修改用户及角色
     */
    @RequestMapping(value = "deleteUser", method = RequestMethod.GET)
    @ResponseBody
    public void deleteUserByuName(String uname) {
        String sql = "update user set is_status='N' where uname=?";
        this.userService.deleteUserByuName(sql, uname);
    }
}
